﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Website/Site1.Master" AutoEventWireup="true" CodeBehind="EditItem.aspx.cs" Inherits="ChicagoDealsN.Website.EditItem" %>
<%@ Import Namespace="ChicagoDealsN" %>
<%@ Import Namespace="System.Data" %>

<script  language = "c#" runat="server">
    public void Page_Load(object sender, EventArgs e)
    {
        //  RESTRICT USER FROM ACCESSING THIS PAGE UNLESS USER IS LOGGED IN !
        if (!IsLoggedIn())
            Response.Redirect("/website/Default.aspx");
        else 
        {
            confirmation.Text = (Session["UserDet"] as DataUser).User_Name +", You can edit your deal here.";    
        }
           
        if (!IsPostBack)
        {
            try
            {
                int itemID = getId();
                String id = Request.QueryString["Deal_Id"];
                //confirmationmessage.Text = "Value = " + id;

               BizDeals myBizObject = new BizDeals();
                
               int UserId = 0;
              DataTable dt = myBizObject.BLGetDealById(itemID);
              string Title="", Companyname="", ImageUrl="", Desc="", OPrice= "", DPrice = "", Strdate = "", Enddate = "", Qty = "";
              int Isactive = 0;
              foreach (DataRow dr in dt.Rows)
              {
                  //txtId.Text = dr["Deal_Id"].ToString();
                  Title = dr["Deal_Title"].ToString();
                  Companyname = dr["Company_Name"].ToString();
                  //FileUploadControl = dr["Img_Path"].ToString(); 
                  ImageUrl = "~" + dr["Img_Path"].ToString();
                  Desc = dr["Deal_Description"].ToString();
                  OPrice = dr["Org_Price"].ToString();
                  DPrice = dr["Deal_Price"].ToString();
                  Strdate = dr["Start_Date"].ToString();
                  Enddate = dr["End_Date"].ToString();
                  Qty = dr["Quantity"].ToString();
                  UserId = int.Parse(dr["User_Id"].ToString());
                  Isactive = int.Parse(dr["Is_Active"].ToString());
              }
              if (Isactive != 0)
              {
                  confirmation.Text = " This deal is already approved <a href='/website/Default.aspx'> Click Here to go Back</a>";
                  tb.Visible = false;
                  // Response.Redirect("/website/Default.aspx");
              }
              else
              {
                  if ((Session["UserDet"] as DataUser).User_Id != UserId)
                  {
                      Response.Redirect("/website/Default.aspx");
                  }
                  else
                  {
                      //txtId.Text = dr["Deal_Id"].ToString();
                      txtTitle.Text = Title;
                      txtCompanyname.Text = Companyname;
                      //FileUploadControl = dr["Img_Path"].ToString(); 
                      dealImg.ImageUrl = "~" + ImageUrl;
                      txtDesc.Text = Desc;
                      txtOPrice.Text = OPrice;
                      txtDPrice.Text = DPrice;
                      calStrdate.Text = Strdate;
                      calEnddate.Text = Enddate;
                      txtQty.Text = Qty;

                  }
               } 
              }
            catch (Exception ex)
            {
                //confirmationmessage.Text = "<h3>Invalid request</h1><br/>" + e + "<br/><br/>";
            }

        }
                
    }
    public void Button_Submit(Object s, EventArgs e)
    {
     try
        {
            BizDeals bu = new BizDeals();
            int itemID = getId();
           // string id = Request.QueryString["Deal_Id"];
            string Imgpath = "";
            if (FileUploadControl.HasFile)
            {
                string filename = System.IO.Path.GetFileName(FileUploadControl.FileName);
                FileUploadControl.SaveAs(Server.MapPath("~/static/img/") + filename);
                Imgpath = "/static/img/" + filename;
                //confirmation.Text = "Your request to new deal has been posted!";
            }

            bool c = bu.BLUpdateDeal(itemID, txtTitle.Text, txtCompanyname.Text, Imgpath, txtDesc.Text, float.Parse(txtOPrice.Text), float.Parse(txtDPrice.Text), calStrdate.Text, calEnddate.Text, Convert.ToInt32(txtQty.Text));
            if (c)
            {
                string message = "<br>Your Details are updated!";
                confirmation.Text = message;

             }
            else
            {
                string message1 = "<br>error!!";
                confirmation.Text = message1;
            }
        }
        catch (Exception exp) { confirmation.Text = exp.ToString(); }
              
        
    }
    
    private bool IsLoggedIn()
    {
        return (Session["UserDet"] != null) ? true : false;
    }

    private int getId()
    {
        return int.Parse(Request.QueryString["id"]);
    }
    
 </script>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder3" runat="server">
<div id="conirmation-container">
       <asp:Label id="confirmation" runat="server" />
       <br/>
</div>

   <table runat="server" id="tb">
    <tr>
        <td>Title:</td>
        <td><asp:TextBox runat="server" ID="txtTitle" /></td>
    </tr>
    <tr>
        <td>Image:</td>
        <td>
            <asp:FileUpload id="FileUploadControl" runat="server" />
            <asp:Image ID="dealImg" runat="server" />
        </td>
    </tr>
    <tr>
        <td>Company Name:</td>
        <td><asp:TextBox runat="server" ID="txtCompanyname" /></td>
    </tr>
    <tr>
        <td>Description: </td>
        <td><asp:TextBox runat="server" ID="txtDesc" /></td>
    </tr>
    <tr>
        <td>Original Price:</td>
        <td><asp:TextBox runat="server" ID="txtOPrice" /></td>
    </tr>
    <tr>
        <td>Deal Price:</td>
        <td><asp:TextBox runat="server" ID="txtDPrice" /></td>
    </tr>
    <tr>
        <td>Start Date:</td>
        <td><asp:TextBox runat="server" ID="calStrdate" /></td>
    </tr>
    <tr>
        <td>End Date:</td>
        <td><asp:TextBox runat="server" ID="calEnddate" /></td>
    </tr>
    <tr>
        <td>Quantity:</td>
        <td><asp:TextBox runat="server" ID="txtQty" /></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td><asp:Button runat="server" ID="submit_newdeal" OnClick="Button_Submit" text="Submit"/></td>
    </tr>
   </table>
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
</asp:Content>
